
#include "rviz_custom_plugins/utils/interactive_marker_manager.h"


namespace rviz_custom_plugins
{

namespace utils
{

// 静态成员初始化
InteractiveMarkerManager* InteractiveMarkerManager::instance = nullptr;
QMutex InteractiveMarkerManager::mutex(QMutex::Recursive);

InteractiveMarkerManager* InteractiveMarkerManager::getInstance() {
    QMutexLocker lock(&mutex);
    if (instance == nullptr) {
        instance = new InteractiveMarkerManager();
    }
    return instance;
}

std::shared_ptr<InteractiveMarkerThread> InteractiveMarkerManager::addThread(QString& taskName)
{
    QMutexLocker lock(&mutex);
    std::shared_ptr<InteractiveMarkerThread> thread = getRunningThread(taskName);
    if(thread == nullptr) {
        thread = std::make_shared<InteractiveMarkerThread>();
        thread->setObjectName(taskName);
        thread->start();
        taskMap.insert(taskName, thread);
    }
	return thread;
}


std::shared_ptr<InteractiveMarkerThread> InteractiveMarkerManager::getRunningThread(QString& taskName) {
    QMutexLocker lock(&mutex);
    std::shared_ptr<InteractiveMarkerThread> thread = taskMap.value(taskName, nullptr);
    if(thread == nullptr) {
        return nullptr;
    }
    if (thread->isRunning()) {
        return thread;
    }
    taskMap.remove(taskName);
    return nullptr;

}

}

}
